﻿Imports System.Data.Objects.DataClasses
Imports DotNetNuke.Services.Tokens
Imports DotNetNuke.Services.FileSystem
Imports System.Web
Imports DotNetNuke.Entities.Portals

Namespace DotNetNuke.Modules.Announcements.Model
    Partial Public Class Announcement
        Implements IPropertyAccess

        Public Property ModuleID() As Integer
            Get
                Dim retval As Integer = -1
                If DnnModuleReference.EntityKey IsNot Nothing Then
                    ' this is only safe because we know Module has a single entitykey propery
                    retval = CInt(DnnModuleReference.EntityKey.EntityKeyValues(0).Value)
                End If
                Return retval
            End Get
            Set(ByVal value As Integer)
                If DnnModuleReference Is Nothing Then
                    DnnModuleReference = New EntityReference(Of [Module])() With {.EntityKey = New EntityKey("AnnouncementsEntities.Modules", "ModuleID", value)}
                Else
                    DnnModuleReference.EntityKey = New EntityKey("AnnouncementsEntities.Modules", "ModuleID", value)
                End If
            End Set
        End Property

        Public Property IsEditable() As Boolean
            Get
                Return m_IsEditable
            End Get
            Set(ByVal value As Boolean)
                m_IsEditable = value
            End Set
        End Property
        Private m_IsEditable As Boolean
        Public Property TrackClicks() As Boolean
            Get
                Return m_TrackClicks
            End Get
            Set(ByVal value As Boolean)
                m_TrackClicks = value
            End Set
        End Property
        Private m_TrackClicks As Boolean
        Public Property NewWindow() As Boolean
            Get
                Return m_NewWindow
            End Get
            Set(ByVal value As Boolean)
                m_NewWindow = value
            End Set
        End Property
        Private m_NewWindow As Boolean


#Region "IPropertyAccess Implementation"

        Private LocalResourceFile As String = ApplicationPath + "/DesktopModules/Announcements/App_LocalResources/Announcements.ascx"


        Public Function GetProperty(ByVal strPropertyName As String, ByVal strFormat As String, ByVal formatProvider As System.Globalization.CultureInfo, ByVal AccessingUser As Entities.Users.UserInfo, ByVal AccessLevel As Services.Tokens.Scope, ByRef PropertyNotFound As Boolean) As String Implements Services.Tokens.IPropertyAccess.GetProperty
            Dim OutputFormat As String = String.Empty
            Dim portalSettings As PortalSettings = PortalController.GetCurrentPortalSettings()
            If strFormat = String.Empty Then
                OutputFormat = "D"
            Else
                OutputFormat = strFormat
            End If
            Select Case strPropertyName.ToLowerInvariant
                Case "edit"
                    If IsEditable Then
                        Return "<a href=""" + DotNetNuke.Common.Globals.NavigateURL(portalSettings.ActiveTab.TabID, False, portalSettings, "Edit", Globalization.CultureInfo.CurrentCulture.Name, "mid=" + ModuleID.ToString, "itemid=" + ItemID.ToString) + """><img border=""0"" src=""" + Common.Globals.ApplicationPath + "/images/edit.gif"" alt=""" + Localization.GetString("EditAnnouncement.Text", LocalResourceFile) + """ /></a>"
                    Else
                        Return String.Empty
                    End If
                Case "itemid"
                    Return (Me.ItemID.ToString(OutputFormat, formatProvider))
                Case "moduleid"
                    Return (Me.ModuleID.ToString(OutputFormat, formatProvider))
                Case "title"
                    Return PropertyAccess.FormatString(Me.Title, strFormat)
                Case "url"
                    If String.IsNullOrEmpty(URL) Then
                        Return PropertyAccess.FormatString(URL, strFormat)
                    Else
                        Return PropertyAccess.FormatString(Utilities.FormatUrl(URL, portalSettings.ActiveTab.TabID, ModuleID, TrackClicks), strFormat)
                    End If
                Case "description"
                    Return HttpUtility.HtmlDecode(Me.Description)
                Case "imagesource", "rawimage"
                    Dim strValue As String = Me.ImageSource
                    If strPropertyName.ToLowerInvariant = "imagesource" AndAlso String.IsNullOrEmpty(strFormat) Then
                        strFormat = "<img src=""{0}"" alt=""" + Title + """/>"
                    End If

                    'Retrieve the path to the imagefile
                    If strValue <> "" Then
                        'Get path from filesystem only when the image comes from within DNN.

                        If Me.ImageSource.StartsWith("FileID=") Then
                            Dim fileCnt As New FileController
                            Dim objFile As FileInfo = fileCnt.GetFileById(CInt(strValue.Substring(7)), portalSettings.PortalId)
                            If objFile IsNot Nothing Then
                                strValue = portalSettings.HomeDirectory & objFile.Folder & objFile.FileName
                            Else
                                strValue = ""
                            End If
                        End If
                        strValue = PropertyAccess.FormatString(strValue, strFormat)
                    End If
                    Return strValue
                Case "vieworder"
                    Return (Me.ViewOrder.ToString(OutputFormat, formatProvider))
                Case "createdbyuser"
                    Dim tmpUser As UserInfo = UserController.GetUser(portalSettings.PortalId, Me.CreatedByUser, False)
                    If Not tmpUser Is Nothing Then
                        Return tmpUser.DisplayName
                    Else
                        Return Localization.GetString("userUnknown.Text", LocalResourceFile)
                    End If
                Case "trackclicks"
                    Return (PropertyAccess.Boolean2LocalizedYesNo(Me.TrackClicks, formatProvider))
                Case "newwindow"
                    If NewWindow Then
                        Return "_new"
                    Else
                        Return "_self"
                    End If
                Case "createddate"
                    Return (Me.CreatedDate.ToString(OutputFormat, formatProvider))
                Case "publishdate"
                    Return (Me.PublishDate.ToString(OutputFormat, formatProvider))
                Case "expiredate"
                    Return (Me.ExpireDate.ToString(OutputFormat, formatProvider))
                Case "more"
                    Return Localization.GetString("More.Text", LocalResourceFile)
                Case "readmore"
                    Dim strTarget As String
                    If NewWindow Then
                        strTarget = "_new"
                    Else
                        strTarget = "_self"
                    End If

                    If Not String.IsNullOrEmpty(URL) Then
                        Return "<a href=""" + Utilities.FormatUrl(URL, portalSettings.ActiveTab.TabID, ModuleID, TrackClicks) + """ target=""" + strTarget + """>" + Localization.GetString("More.Text", LocalResourceFile) + "</a>"
                    Else
                        Return ""
                    End If

                Case Else
                    PropertyNotFound = True
            End Select

            Return Null.NullString
        End Function

        Public ReadOnly Property Cacheability() As Services.Tokens.CacheLevel Implements Services.Tokens.IPropertyAccess.Cacheability
            Get
                Return CacheLevel.fullyCacheable
            End Get
        End Property

#End Region

    End Class
End Namespace